package com.tools.s7.network.support;

import com.tools.s7.network.listener.OnProgressListener;

import java.io.File;
import java.io.IOException;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okio.Buffer;
import okio.BufferedSink;
import okio.ForwardingSink;
import okio.Okio;
import okio.Sink;


public class UploadFileRequestBody extends RequestBody {

    private RequestBody mRequestBody;
    private OnProgressListener mProgressListener;

    private BufferedSink bufferedSink;

    public UploadFileRequestBody(File file , OnProgressListener progressListener) {
        this.mRequestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file) ;
        this.mProgressListener = progressListener ;
    }

    public UploadFileRequestBody(RequestBody requestBody, OnProgressListener progressListener) {
        this.mRequestBody = requestBody;
        this.mProgressListener = progressListener;
    }

    @Override
    public MediaType contentType() {
        return mRequestBody.contentType();
    }


    @Override
    public long contentLength() throws IOException {
        return mRequestBody.contentLength();
    }

    @Override
    public void writeTo(BufferedSink sink) throws IOException {
        if (bufferedSink == null) {

            bufferedSink = Okio.buffer(sink(sink));
        }

        mRequestBody.writeTo(bufferedSink);

        bufferedSink.flush();
    }

    private Sink sink(Sink sink) {
        return new ForwardingSink(sink) {

            long bytesWritten = 0L;
            long contentLength = 0L;

            @Override
            public void write(Buffer source, long byteCount) throws IOException {
                super.write(source, byteCount);
                if (contentLength == 0) {

                    contentLength = contentLength();
                }

                bytesWritten += byteCount;

                if (mProgressListener != null) {
                    mProgressListener.onProgress(bytesWritten, contentLength, bytesWritten == contentLength);
                }
            }
        };
    }
}
